package com.agent.auth.mapper;

import com.agent.auth.entity.UserSession;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户会话Mapper
 * @author Jeff_Wan
 * @description 操作用户会话数据
 */
@Mapper
public interface UserSessionMapper extends BaseMapper<UserSession> {

    @Select("SELECT * FROM user_session WHERE session_token = #{token}")
    UserSession findByToken(@Param("token") String token);

    @Select("SELECT * FROM user_session WHERE refresh_token = #{token}")
    UserSession findByRefreshToken(@Param("token") String token);

    @Select("SELECT * FROM user_session WHERE expires_at < #{now} AND is_active = true")
    List<UserSession> findExpiredSessions(@Param("now") LocalDateTime now);

    @Update("UPDATE user_session SET is_active = false WHERE session_token = #{token}")
    int deactivateSession(@Param("token") String token);
}